package cn.itqiqi.student_system_java.service.impl;

import cn.hutool.json.JSONUtil;
import cn.itqiqi.student_system_java.mapper.StudentMapper;
import cn.itqiqi.student_system_java.pojo.Student;
import cn.itqiqi.student_system_java.service.StudentService;
import cn.itqiqi.student_system_java.utils.ResultUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description
 * @date 2022-06-19 15:59
 */

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ResultUtils resultUtils;

    @Override
    public String insertStu(String student) {
        try {
            Student studentBean = JSONUtil.toBean(student, Student.class);
            studentMapper.insertStu(studentBean);
            return resultUtils.success("添加成功！");
        } catch (Exception e) {
            return resultUtils.fail(500, "添加失败！");
        }
    }

    @Override
    public String updateStu(String student) {
        try {
            Student studentBean = JSONUtil.toBean(student, Student.class);
            studentMapper.updateStu(studentBean);
            return resultUtils.success("修改成功！");
        } catch (Exception e) {
            return resultUtils.fail(500, "修改失败！");
        }
    }

    @Override
    public String deleteStu(String student) {
        try {
            Student studentBean = JSONUtil.toBean(student, Student.class);
            studentMapper.deleteStu(studentBean.getSid());
            return resultUtils.success("删除成功！");
        } catch (Exception e) {
            return resultUtils.fail(500, "删除失败！");
        }
    }
}
